package games.gong.durid.mapper;

import games.gong.durid.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper {
    
    @Insert("INSERT INTO role(name, description, create_time, update_time) " +
            "VALUES(#{name}, #{description}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Role role);

    @Delete("DELETE FROM role WHERE id = #{id}")
    int deleteById(Long id);

    @Update("UPDATE role SET name=#{name}, description=#{description}, update_time=#{updateTime} WHERE id = #{id}")
    int update(Role role);

    @Select("SELECT * FROM role WHERE id = #{id}")
    @Results({
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    Role selectById(Long id);

    @Select("SELECT * FROM role")
    @Results({
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<Role> selectAll();
    
    // 根据用户ID查询角色
    @Select("SELECT r.* " +
            "FROM role r " +
            "INNER JOIN user_role ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    @Results({
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<Role> selectRolesByUserId(Long userId);

    // 根据角色ID查询权限
    @Select("SELECT p.* " +
            "FROM permission p " +
            "INNER JOIN role_permission rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId}")
    @Results({
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<games.gong.durid.entity.Permission> selectPermissionsByRoleId(Long roleId);
}